package com.hyy.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyy.api.domain.provider.ProvProviderParam;
import com.hyy.api.domain.provider.bo.ProvProviderParamBo;
import com.hyy.api.domain.provider.vo.ProvProviderParamVo;
import com.hyy.common.core.utils.MapstructUtils;
import com.hyy.common.core.utils.StringUtils;
import com.hyy.common.mybatis.core.page.PageQuery;
import com.hyy.common.mybatis.core.page.TableDataInfo;
import com.hyy.provider.mapper.ProvProviderParamMapper;
import com.hyy.provider.service.IProvProviderParamService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 供应商连接程序参数模板Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-20
 */
@RequiredArgsConstructor
@Service
public class ProvProviderParamServiceImpl implements IProvProviderParamService {

    private final ProvProviderParamMapper baseMapper;

    /**
     * 查询供应商连接程序参数模板
     *
     * @param id 主键
     * @return 供应商连接程序参数模板
     */
    @Override
    public ProvProviderParamVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询供应商连接程序参数模板列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 供应商连接程序参数模板分页列表
     */
    @Override
    public TableDataInfo<ProvProviderParamVo> queryPageList(ProvProviderParamBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProvProviderParam> lqw = buildQueryWrapper(bo);
        Page<ProvProviderParamVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的供应商连接程序参数模板列表
     *
     * @param bo 查询条件
     * @return 供应商连接程序参数模板列表
     */
    @Override
    public List<ProvProviderParamVo> queryList(ProvProviderParamBo bo) {
        LambdaQueryWrapper<ProvProviderParam> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProvProviderParam> buildQueryWrapper(ProvProviderParamBo bo) {
        LambdaQueryWrapper<ProvProviderParam> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ProvProviderParam::getId);
        lqw.eq(bo.getProviderId() != null, ProvProviderParam::getProviderId, bo.getProviderId());
        lqw.like(StringUtils.isNotBlank(bo.getFieldName()), ProvProviderParam::getFieldName, bo.getFieldName());
        lqw.eq(StringUtils.isNotBlank(bo.getFieldValue()), ProvProviderParam::getFieldValue, bo.getFieldValue());
        return lqw;
    }

    /**
     * 新增供应商连接程序参数模板
     *
     * @param bo 供应商连接程序参数模板
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ProvProviderParamBo bo) {
        ProvProviderParam add = MapstructUtils.convert(bo, ProvProviderParam.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改供应商连接程序参数模板
     *
     * @param bo 供应商连接程序参数模板
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ProvProviderParamBo bo) {
        ProvProviderParam update = MapstructUtils.convert(bo, ProvProviderParam.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProvProviderParam entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除供应商连接程序参数模板信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void saveBatch(Long providerId, List<ProvProviderParamBo> entityList) {
        //1、先删除
        LambdaQueryWrapper<ProvProviderParam> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProvProviderParam::getProviderId, providerId);
        baseMapper.delete(lqw);
        //2、再保存
        List<ProvProviderParam> convert = MapstructUtils.convert(entityList, ProvProviderParam.class);
        for (ProvProviderParam provProviderParam : convert) {
            provProviderParam.setProviderId(providerId);
        }
        baseMapper.insertBatch(convert);
    }

    @Override
    public List<ProvProviderParamVo> queryListByProviderId(Long providerId) {
        LambdaQueryWrapper<ProvProviderParam> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProvProviderParam::getProviderId, providerId);
        return baseMapper.selectVoList(lqw);

    }
}
